(function(){"use strict";BX.namespace("BX.Main.ui");BX.Main.ui.CustomEntity=function(){this.field=null;this.labelInput=null;this.hiddenInput=null;this.popupContainer=null;this.inputClass="main-ui-control-string";this.squareClass="main-ui-square";this.multiple=null};BX.Main.ui.CustomEntity.isMultiple=function(t){if(!!t&&!BX.hasClass(t,"main-ui-control-entity")){t=BX.Filter.Utils.getByClass(t,"main-ui-control-entity")}return!!t&&JSON.parse(BX.data(t,"multiple"))};BX.Main.ui.CustomEntity.prototype={setField:function(t){if(this.field!==t){this.field=t;this.reset()}},isMultiple:function(){return BX.Main.ui.CustomEntity.isMultiple(this.getField())},reset:function(){this.labelInput=null;this.hiddenInput=null},getField:function(){return this.field},getId:function(){var t=this.getHiddenNode();var e=null;if(BX.type.isDomNode(t)){e=t.name}return e},getLabelNode:function(){if(!BX.type.isDomNode(this.labelInput)){this.labelInput=BX.Filter.Utils.getBySelector(this.getField(),"."+this.inputClass+'[type="text"]')}return this.labelInput},getHiddenNode:function(){if(!BX.type.isDomNode(this.hiddenInput)){this.hiddenInput=BX.Filter.Utils.getBySelector(this.getField(),"."+this.inputClass+'[type="hidden"]')}return this.hiddenInput},getSquareByValue:function(t){return BX.Filter.Utils.getBySelector(this.getField(),['[data-item*=":'+BX.util.jsencode(t)+'}"]','[data-item*=":\\"'+BX.util.jsencode(t)+'\\"}"]'].join(", "))},getSquares:function(){return BX.Filter.Utils.getByClass(this.getField(),this.squareClass,true)},removeSquares:function(){this.getSquares().forEach(BX.remove)},setSquare:function(t,e){var i=this.getField();var n={block:"main-ui-square",name:t,item:{_label:t,_value:e}};var s=BX.decl(n);var l=this.getSquares();if(!l.length){BX.prepend(s,i)}else{BX.insertAfter(s,l[l.length-1])}},getCurrentValues:function(){var t=this.getSquares();var e,i;if(this.isMultiple()){i=[];for(var n=0,s=t.length;n<s;n++){try{e=JSON.parse(BX.data(t[n],"item"));i.push({label:e._label,value:e._value})}catch(t){}}}else{if(t.length===0){i={label:"",value:""}}else{try{e=JSON.parse(BX.data(t[0],"item"));i={label:e._label,value:e._value}}catch(t){i={label:"",value:""}}}}return i},setData:function(t,e){return this.isMultiple()?this.setMultipleData(t,e):this.setSingleData(t,e)},setSingleData:function(t,e){var i=this.getHiddenNode();this.removeSquares();this.setSquare(t,e);if(BX.type.isDomNode(i)){i.value=e;BX.fireEvent(i,"input")}},setMultipleData:function(t,e){var i=[];var n=this.getHiddenNode();if(BX.type.isArray(t)){this.removeSquares();if(BX.type.isArray(t)){t.forEach(function(t){i.push(t.value);this.setSquare(t.label,t.value)},this);if(BX.type.isDomNode(n)){n.value=JSON.stringify(i);BX.fireEvent(n,"input")}}}if(!BX.type.isArray(t)&&e!==null){if(!this.getSquareByValue(e)){this.setSquare(t,e);this.getSquares().forEach(function(t){var e=JSON.parse(BX.data(t,"item"));if(BX.type.isPlainObject(e)){i.push(e._value)}});n.value=JSON.stringify(i);BX.fireEvent(n,"input")}}},setPopupContainer:function(t){if(BX.type.isDomNode(t)){this.popupContainer=t}},getPopupContainer:function(){return this.popupContainer}}})();